package org.fessingapore.library.controllers;

import java.util.List;

import org.fessingapore.library.models.Group;
import org.fessingapore.library.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/group")
public class GroupController {

	private GroupService groupService;

	@Autowired
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String searchAll(Model model) {
		List<Group> groupList = groupService.findAll();
		model.addAttribute("groupList", groupList);
		model.addAttribute("group", new Group());
		return "config/searchGroup";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	public String addGroup(Group group, Model model) {
		groupService.create(group);
		return "redirect:/group";
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteGroup(@PathVariable String id, Model model) {
		
		if (!id.equals("")){
			Group group = groupService.getById(Integer.parseInt(id));
			groupService.delete(group);
		}
		return "redirect:/group";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updateGroup(Group group, Model model) {
		
		groupService.update(group);
	
		return "redirect:/group";
	}

	
}
